using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ChangePassword : System.Web.UI.Page
{
    void Page_PreInit(Object sender, EventArgs e)
    {
        if (User.IsInRole("Admin"))
        {
            this.MasterPageFile = "Admin.master";
        }
        else if (User.IsInRole("Employee"))
        {
            this.MasterPageFile = "Employee.master";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        txtOldPassword.Focus();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtConfirmPassword.Text = "";
        txtOldPassword.Focus();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                //Get password of the logged in user
                string Password = DALChangePassword.GetPassword(User.Identity.Name);
                //Compare the old password and the user's password
                if (txtOldPassword.Text.Trim().Equals(Password))
                {
                    if (DALChangePassword.UpdatePassword(txtNewPassword.Text.Trim(), User.Identity.Name))
                    {
                        Label2.Text = "Password changed successfully.";
                    }
                    else
                    {
                        Label2.Text = "Error updating password!";
                    }
                }
                else
                {
                    Label2.Text = "Please check your old password!";
                }
            }
            catch
            {
                Label2.Text = "Error updating password!";
            }
        }
    }
}
